layui.config({
    base: '/static/admin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use('index');

// js操作方法
layui.use(['jquery', 'form', 'layer', 'laydate'], function () {
    // use layer,
    let $ = layui.jquery, // jquery
        form = layui.form,
        layer = layui.layer,
        laydate = layui.laydate;
    // ============时间============
    //常规用法
    laydate.render({
        elem: '#start_date'
    });
    //常规用法
    laydate.render({
        elem: '#end_date'
    });
    //时间选择器
    laydate.render({
        elem: '#start_time'
        ,type: 'datetime'
    });
    //时间选择器
    laydate.render({
        elem: '#end_time'
        ,type: 'datetime'
    });
    //时间选择器
    laydate.render({
        elem: '#time'
        ,type: 'time'
    });
    //时间范围
    laydate.render({
        elem: '#range_date'
        ,range: true
    });
    // ============时间============
    // 全选
    form.on('checkbox(allChoose)', function(data) {
        let child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
        child.each(function(index, item) {
            item.checked = data.elem.checked;
        });
        form.render('checkbox');
    });
    form.on('checkbox(checkAll)', function() {
        let child = $(this).attr('rel');
        $(".child_" + child).prop('checked', $(this).prop("checked"));
        form.render('checkbox');
    });
    // 操作
    $(document).on("click", "a[lay-event='act']", function (e) {
        e.preventDefault();
        let obj = $(this);
        layer.confirm('您确定要' + obj.text() + '吗？', {
            btn: ['确定','取消'] //按钮
        }, function(index){
            $("#hide_form").attr('src', obj.attr('data-url'));
            setTimeout(function () {
                refresh();
                layer.close(index);
            }, 1000);
        });
    });
    // 批量操作
    $(document).on('click', "a[lay-event='list']", function (e) {
        e.preventDefault();
        let obj = $(this);
        layer.confirm('您确定要' + obj.html(), {
            btn: ['确定','取消'] //按钮
        }, function(index){
            obj.parents('form').attr('action', obj.attr('data-url')).submit();
            layer.close(index);
        });
    });
    // 新窗口打开
    $(document).on("click", "a[lay-event='load']", function (e) {
        e.preventDefault();
        let obj = $(this);
        page(obj.text(), obj.attr('data-url'), obj.attr('w'),  obj.attr('h'));
    });
    // 弹窗选择
    $(document).on("click", "#select_btn", function (e) {
        $("input[name='member_id']").each(function (a) {
            if ($(this).prop("checked") == true) {
                selectCallBack('member_id', 'nickname', $(this).val(), $(this).attr('rel'));
                selectCallBack('target_id', 'target_name', $(this).val(), $(this).attr('rel'));
            }
        });
        $("input[name='admin_id']").each(function (a) {
            if ($(this).prop("checked") == true) {
                selectCallBack('admin_id', 'nickname', $(this).val(), $(this).attr('rel'));
                selectCallBack('target_id', 'target_name', $(this).val(), $(this).attr('rel'));
            }
        });
        $("input[name='area_id']").each(function (a) {
            if ($(this).prop("checked") == true) {
                selectCallBack('area_id', 'area_name', $(this).val(), $(this).attr('rel'));
            }
        });
    });
    // 图片上传
    $(document).ready(function(e) {
        $('#upload_file').on('change', function(){
            upload(uploadUrl);
        });
    });
    /*// F12调试模式窗口自适应
    $(window).on("resize",function(){
        if (index !== undefined){
            layer.full(index);
        }
    })*/
});

// 图片上传
function upload(uploadUrl){
    if (!window.FormData){
        return false;
    }
    // HTML5 新增对象
    let formData = new FormData(document.getElementById('uploadForm'));
    //添加其他表单域
    let xhr = new XMLHttpRequest();
    xhr.open('POST', uploadUrl); //请求url

    //上传完成回调函数
    xhr.onload = function(event) {
        let data = xhr.response;
        // let data = JSON.parse(str);
        if (data !== ''){
            if(data.split('/').length > 1){
                result(into, data);
            }else{
                layer.msg(data);
            }
        }else{
            layer.msg('图片上传失败');
        }
    };
    xhr.send(formData);
}

let index;

function page(title, url, w, h) {
    if(title == null || title === '') {
        title = false;
    }
    if(url == null || url === '') {
        url = "404.html";
    }
    if (w === undefined && h === undefined){
        index = layer.open({
            type: 2,
            title: title,
            content: url
        });
        layer.full(index);
    }else{
        index = layer.open({
            type: 2,
            title: title,
            content: url,
            area: [w + 'px', h + 'px']
        });
    }
}

// 刷新当前窗口
function refresh(url) {
    // 获取当前iframe的索引
    let liObj = parent.window.document.getElementById('LAY_app_tabsheader').getElementsByTagName('li');
    let index = 0;
    for (let i = 0;i < liObj.length; i++){
        if (liObj.item(i).className === 'layui-this'){
            index = i;
        }
    }
    // 有隐藏时frame需要增加
    index++;
    if (url){
        parent.window.frames[index].location.href = url;
    }else{
        parent.window.frames[index].location.reload();
    }
}

function success(msg, timeout, callback) {
    parent.layer.msg(msg, {icon: 1,time: timeout});
    eval(callback);
}

function error(msg, timeout, close) {
    parent.layer.msg(msg, {time: timeout});
    if (close === 'on'){
        parent.layer.close(index);
    }
}

function jumpUrl(url, close) {
    if (url !== '') {
        refresh(url);
    } else {
        if (close === 'on'){
            refresh();
            setTimeout(function () {
                parent.layer.close(index);
            }, 1000)
        }
    }
}

// 选择后返回数据
function selectCallBack(id = '', name = '', v1 = '', v2 = '') {
    let obj = parent.window.document;
    let is_close = false;
    let id_value = obj.getElementById(id);
    if (id_value != null){
        id_value.value = v1;
        is_close = true;
    }
    let name_value = obj.getElementById(name);
    if (name_value != null){
        name_value.value = v2;
        is_close = true;
    }
    if (is_close){
        let layer_index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(layer_index);
    }
}
function selectCallBacks(id = '', name = '', v1 = '', v2 = '') {
    // 获取当前iframe的索引
    let liObj = parent.window.document.getElementById('LAY_app_tabsheader').getElementsByTagName('li');
    let windowIndex = 0;
    for (let i = 0;i < liObj.length; i++){
        if (liObj.item(i).className === 'layui-this'){
            windowIndex = i;
        }
    }
    // 因为有隐藏时frame需要增加
    windowIndex++;
    let obj = parent.window.frames[windowIndex].document;
    let is_close = false;
    let id_value = obj.getElementById(id);
    if (id_value != null){
        id_value.value = v1;
        is_close = true;
    }
    let name_value = obj.getElementById(name);
    if (name_value != null){
        name_value.value = v2;
        is_close = true;
    }
    if (is_close){
        let layer_index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(layer_index);
    }
}

function clearNoNum(obj, min, max){
    obj.value = obj.value.replace(/[^\d.]/g,"");
    obj.value = obj.value.replace(/^\./g,"");  //验证第一个字符是数字而不是.
    obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的.
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
    if (min > 0){
        if (obj.value < min){
            obj.value = min;
        }
    }
    if (max > 0){
        if (obj.value > max){
            obj.value = max;
        }
    }
}
